package com.pie4cloud.pie.job.biz.service.execute.impl;


import com.alibaba.fastjson.JSON;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.pie4cloud.pie.common.core.constant.SecurityConstants;
import com.pie4cloud.pie.common.core.util.R;
import com.pie4cloud.pie.common.core.util.RedisUtil;
import com.pie4cloud.pie.job.api.dto.CallBackDto;
import com.pie4cloud.pie.job.api.dto.SystemInfoDto;
import com.pie4cloud.pie.job.api.entity.DmzEntity;
import com.pie4cloud.pie.job.api.entity.JobInfoEntity;
import com.pie4cloud.pie.job.api.feign.RemoteMonitorService;
import com.pie4cloud.pie.job.biz.config.FeignRemote;
import com.pie4cloud.pie.job.biz.enums.ExecutorBlockStrategyEnum;
import com.pie4cloud.pie.job.biz.service.DmzService;
import com.pie4cloud.pie.job.biz.service.execute.ExecuteService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Slf4j
@RequiredArgsConstructor
@Service("executeDmzService")
public class ExecuteDmzService extends ExecuteBaseService implements ExecuteService {
	public static final ExecutorService sendLocalPool = new ThreadPoolExecutor(30, 30,
			0L, TimeUnit.MILLISECONDS,
			new LinkedBlockingQueue<Runnable>(20000), new ThreadFactoryBuilder().setNameFormat("do-send-local-pool-%d").build(), new ThreadPoolExecutor.AbortPolicy());
    @Autowired
    private DmzService dmzService;
	@Autowired
	private FeignRemote feignRemote;
	@Autowired
	private RedisUtil redisUtil;

	private static String monitorDetail="MONITOR_DETAIL";


    @Override
    public JobInfoEntity getById(Long id) {
		DmzEntity dmzEntity= dmzService.getById(id);
        return dmzEntity;
    }

    @Override
    public void checkExecutorBlockStrategyEnum(JobInfoEntity jobInfoEntity) {
        jobInfoEntity.setExecutorBlockStrategy(ExecutorBlockStrategyEnum.TASK_SERIAL.name());
    }

	@Override
	public void remote(JobInfoEntity jobInfoEntity, R<String> resultT) {
		sendLocalPool.execute(
				() -> {
					DmzEntity dmzEntity= (DmzEntity) jobInfoEntity;
					SystemInfoDto systemInfoDto=new SystemInfoDto();
					systemInfoDto.setIsUsable(1);
					try {
						RemoteMonitorService remoteMonitorService= feignRemote.target(RemoteMonitorService.class,dmzEntity.getHost()+":"+dmzEntity.getPort());
						R<SystemInfoDto> systemInfoDtoR=remoteMonitorService.getSystemInfo(SecurityConstants.FROM_IN);


						if(systemInfoDtoR.getCode()==0){
							systemInfoDto=systemInfoDtoR.getData();
							dmzEntity.setSystemInfoDto(systemInfoDto);
							redisUtil.hset(monitorDetail,String.valueOf(dmzEntity.getId()),JSON.toJSONString(dmzEntity));
						}else {
							dmzEntity.setSystemInfoDto(systemInfoDto);
							redisUtil.hset(monitorDetail,String.valueOf(dmzEntity.getId()),JSON.toJSONString(dmzEntity));
						}
					} catch (Exception e) {
						dmzEntity.setSystemInfoDto(systemInfoDto);
						redisUtil.hset(monitorDetail,String.valueOf(dmzEntity.getId()),JSON.toJSONString(dmzEntity));
					}
				});

	}

	@Override
	public void callBack(CallBackDto callBackDto) {

	}
}

